﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
using Manager.App_Code.DTO;
using Manager.App_Code;
using System.Linq;
using Manager.Common;


namespace Manager.App_Code.REPORTS
{
    public partial class RP_HoaDonTraPhong : DevExpress.XtraReports.UI.XtraReport
    {
        NewPearl_HotelDataContext db = new NewPearl_HotelDataContext();
        string _TenKhachHang, _MaPhong, _NgayDen, _NgayDi, _SoNgayO, _GiaPhong, _MaTienTe, _MaTienTe_TienCoc, _gioVao, _gioRa;
        double _TongTienVND, _TongTienUSD, tienCoc;
        NguoiDung _nguoiDung;
        NguoiDung _nguoiLapPhieu;
        int stt = 0;

        public RP_HoaDonTraPhong()
        {
            InitializeComponent();
        }
        
        public RP_HoaDonTraPhong(string GuestName, string RoomID, string Arivaldate, string DepatureDate, string DateNumber, string Price)
        {
            InitializeComponent();

            _TenKhachHang = GuestName;
            _MaPhong = RoomID;
            _NgayDen = Arivaldate;
            _NgayDi = DepatureDate;
            _SoNgayO = DateNumber;
            _GiaPhong = Price;
        }        

        public RP_HoaDonTraPhong(CheckOut checkOut, double _tienCoc, string maTienTe, string gioVao, string gioRa, NguoiDung nguoiDung, NguoiDung nguoiLapPhieu)
        {
            InitializeComponent();
            _nguoiDung = nguoiDung;
            _nguoiLapPhieu = nguoiLapPhieu;
            tienCoc = _tienCoc;
            _MaTienTe_TienCoc = maTienTe;
            _TenKhachHang = checkOut.PhieuDangKyPhong.KhachHang.TenKH;
            _MaPhong = checkOut.MaPhong;
            _gioVao = gioVao;
            _gioRa = gioRa;
            var ngayDen = checkOut.ChiTiet_CheckOuts.First().NgayDen.Value;
            var ngayDi = checkOut.ChiTiet_CheckOuts.First().NgayDi.Value;
            _NgayDen = ngayDen.ToString("dd/MM/yyyy");
            _NgayDi = ngayDi.ToString("dd/MM/yyyy");
            _SoNgayO = (ngayDi - ngayDen).Days.ToString();
            _GiaPhong = CommonFunction.AddSeparator(checkOut.ChiTiet_CheckOuts.First().GiaTien.Value);
            _MaTienTe = checkOut.ChiTiet_CheckOuts.First().MaTienTe;
            foreach (var chiTiet in checkOut.ChiTiet_CheckOuts)
            {
                if (chiTiet.MaTienTe == "VND")
                    _TongTienVND += chiTiet.GiaTien.Value;
                else if (chiTiet.MaTienTe == "USD")
                    _TongTienUSD += chiTiet.GiaTien.Value;
            }
            var lstPhuThuDichVu = db.PhuThuDichVus.Where(t => t.MaPhieuDangKy == checkOut.MaPhieuDangKy).ToList();
            foreach (var phuThu in lstPhuThuDichVu)
            {
                if (phuThu.MaTienTe == "VND")
                    _TongTienVND += (phuThu.GiaTien.Value * phuThu.SoLuong.Value);
                else if (phuThu.MaTienTe == "USD")
                    _TongTienUSD += (phuThu.GiaTien.Value * phuThu.SoLuong.Value);
            }
            if (maTienTe == "VND")
                _TongTienVND -= tienCoc;
            else if (maTienTe == "USD")
                _TongTienUSD -= tienCoc;
        }

        public void BindData()
        {
            try
            {
                colTenDichVu.DataBindings.Add("Text", DataSource, "TenDichVu");
                colSoLuong.DataBindings.Add("Text", DataSource, "SoLuong");
                colGiaDichVu.DataBindings.Add("Text", DataSource, "strGiaDV");
                colTienTe.DataBindings.Add("Text", DataSource, "DonViTienTe");
            }
            catch
            { 
                
            }
        }
        
        private void ReportHeader_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            lblTenKhachHang.Text = _TenKhachHang;
            lblMaPhong.Text = _MaPhong;
            lblNgayDen.Text = _NgayDen;
            lblNgayDi.Text = _NgayDi;
            lblGioVao.Text = _gioVao;
            lblGioRa.Text = _gioRa;            
        }

        private void Detail_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            stt++;
            colSTT.Text = stt.ToString();
        }

        private void ReportFooter_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
        {
            lblNguoiDung.Text = _nguoiDung.HoTen;
            lblNguoiLapPhieu.Text = _nguoiLapPhieu.HoTen;
            lblTongTienVND.Text = CommonFunction.AddSeparator(_TongTienVND) + " đ";
            lblTongTienUSD.Text = "$ " + CommonFunction.AddSeparator(_TongTienUSD);
            if (_MaTienTe_TienCoc == "VND")
                lblTraTruoc.Text = CommonFunction.AddSeparator(tienCoc) + " đ";
            else if (_MaTienTe_TienCoc == "USD")
                lblTraTruoc.Text = "$ " + CommonFunction.AddSeparator(tienCoc);
        }

    }
}
